package com.xiangxiangjd.serviceImpl;

import com.xiangxiangjd.controller.helper.$;
import com.xiangxiangjd.bean.Goods;
import com.xiangxiangjd.mapper.GoodsMapper;
import com.xiangxiangjd.service.GoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiangxiangjd.bean.result.ResultPage;
import com.xiangxiangjd.controller.model.ApiResultModel;

import com.xiangxiangjd.utils.StringUtilsXiao;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 物品表 服务实现类
 *
 * @author 肖守凯
 * @since 2021-11-08
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
  /**
   * 添加Goods
   *
   * @param goods 前端传进的参数
   * @return 返回统一信息
   */
  @Override
  public ApiResultModel addGoods(Goods goods) {
    if (this.save(goods)) {
      return $.success();
    } else {
      return $.error("添加失败");
    }
  }
  /**
   * 编辑跳转页面的回显数据Goods
   *
   * @param id 前端传进的参数
   * @return 返回实体类
   */
  @Override
  public Goods selectGoodsByIdGoods(String id) {
    return this.getById(id);
  }

  /**
   * 编辑Goods
   *
   * @param goods 前端传进的参数
   * @return 返回统一信息
   */
  @Override
  public ApiResultModel editGoods(Goods goods) {
    if (this.updateById(goods)) {
      return $.success();
    } else {
      return $.error("编辑失败");
    }
  }
  /**
   * 删除一条Goods
   *
   * @param ids 前端传进的ID
   * @return 返回统一信息
   */
  @Override
  public ApiResultModel delGoodss(List ids) {

    int i = baseMapper.deleteBatchIds(ids);

    return $.success(i);
  }

  /**
   * 分页查询Goods
   *
   * @param page 页码
   * @param limit 查几条
   * @param goods 前端传进的参数
   * @return 返回统一分页信息
   */
  @Override
  public ResultPage findGoodsByPage(Integer page, Integer limit, Goods goods) {

    System.out.println(goods);

    Map<String, Object> paramMap = new HashMap<>();
    page = page == null ? 1 : page;
    limit = limit == null ? 20 : limit;

    paramMap.put("page", (page - 1) * limit);
    paramMap.put("limit", limit);
    paramMap.put("goods", goods);

    if (goods.getDateNo() != null) {

      if (!StringUtilsXiao.isBlank(goods.getDateNo().getPurchaseDate())) {

        String[] purchaseDate = goods.getDateNo().getPurchaseDate().split(",");

        if (purchaseDate[0].equals(purchaseDate[1])) {
          purchaseDate[1] =
              String.valueOf((Long.valueOf(purchaseDate[1]) + 24 * 60 * 60 * 1000) - 1);
        }

        paramMap.put("purchaseDateStart", purchaseDate[0]);
        paramMap.put("purchaseDateEnd", purchaseDate[1]);
      }

      if (!StringUtilsXiao.isBlank(goods.getDateNo().getCreateTime())) {
        String[] createTime = goods.getDateNo().getCreateTime().split(",");

        if (createTime[0].equals(createTime[1])) {
          createTime[1] = String.valueOf((Long.valueOf(createTime[1]) + 24 * 60 * 60 * 1000) - 1);
        }
        paramMap.put("createTimeStart", createTime[0]);
        paramMap.put("createTimeEnd", createTime[1]);
      }
    }
    paramMap.put("page", (page - 1) * limit);
    paramMap.put("limit", limit);
    paramMap.put("goods", goods);
    List<Goods> list = baseMapper.selectMyPage(paramMap);
    Integer count = baseMapper.countMyPage(paramMap);
    ResultPage<Goods> resultPage = new ResultPage<>();
    resultPage.setCode(200);
    resultPage.setCount(count);
    resultPage.setMsg("成功");
    resultPage.setData(list);
    return resultPage;
  }

  @Override
  public ResultPage findByUserAllGoods(Goods goods,String purchaseDate, Integer page, Integer limit) {

    Map<String, Object> paramMap = new HashMap<>();

    paramMap.put("page", (page - 1) * limit);
    paramMap.put("limit", limit);
    paramMap.put("goods", goods);
//    paramMap.put("model", model);
//    paramMap.put("goodsName", goodsName);
//    paramMap.put("unitId", unitId);

    if (!StringUtilsXiao.isBlank(purchaseDate)) {
      String[] createTime = purchaseDate.split(",");

      if (createTime[0].equals(createTime[1])) {
        createTime[1] = String.valueOf((Long.valueOf(createTime[1]) + 24 * 60 * 60 * 1000) - 1);
      }
      paramMap.put("purchaseDate", purchaseDate);
      paramMap.put("createTimeStart", createTime[0]);
      paramMap.put("createTimeEnd", createTime[1]);
    }

    List<Goods> list = baseMapper.selectByUserAllGoods(paramMap);
    Integer count = baseMapper.countByUserAllGoods(paramMap);

    ResultPage<Goods> resultPage = new ResultPage<>();
    resultPage.setCode(200);
    resultPage.setCount(count);
    resultPage.setMsg("成功");
    resultPage.setData(list);
    return resultPage;
  }


  //批量插入

  @Override
  public int insertBatch(List<Goods> goodsList) {


    return baseMapper.insertBatch(goodsList);
  }

  @Override
  public ResultPage getInventoryInfo(Integer page, Integer limit, String userId,String unitId,String pLiable,String purchaseDate,String createTime) {


    Map<String, Object> paramMap = new HashMap<>();
    page = page == null ? 1 : page;
    limit = limit == null ? 20 : limit;



      if (!StringUtilsXiao.isBlank(purchaseDate)) {

        String[] split = purchaseDate.split(",");

        if (split[0].equals(split[1])) {
          split[1] =
                  String.valueOf((Long.valueOf(split[1]) + 24 * 60 * 60 * 1000) - 1);
        }

        paramMap.put("purchaseDateStart", split[0]);
        paramMap.put("purchaseDateEnd", split[1]);
      }

      if (!StringUtilsXiao.isBlank(createTime)) {
        String[] createTimeList = createTime.split(",");

        if (createTimeList[0].equals(createTimeList[1])) {
          createTimeList[1] = String.valueOf((Long.valueOf(createTimeList[1]) + 24 * 60 * 60 * 1000) - 1);
        }
        paramMap.put("createTimeStart", createTimeList[0]);
        paramMap.put("createTimeEnd", createTimeList[1]);
      }


    paramMap.put("page", (page - 1) * limit);
    paramMap.put("limit", limit);
    paramMap.put("userId", userId);
    paramMap.put("pLiable", pLiable);
    paramMap.put("unitId", unitId);


    List<Goods> list = baseMapper.inventoryInfo(paramMap);
    Integer count = baseMapper.countInventoryInfo(paramMap);
    ResultPage<Goods> resultPage = new ResultPage<>();
    resultPage.setCode(200);
    resultPage.setCount(count);
    resultPage.setMsg("成功");
    resultPage.setData(list);
    return resultPage;
  }

  @Override
  public List<Goods> loseMoneyGoods(String userId, List<String> unitIds, String pLiable, String depositAddress, String createTime, String purchaseDate) {



    Map<String, Object> paramMap = new HashMap<>();


    if (!StringUtilsXiao.isBlank(purchaseDate)) {

      String[] split = purchaseDate.split(",");

      if (split[0].equals(split[1])) {
        split[1] =
                String.valueOf((Long.valueOf(split[1]) + 24 * 60 * 60 * 1000) - 1);
      }

      paramMap.put("purchaseDateStart", split[0]);
      paramMap.put("purchaseDateEnd", split[1]);
    }

    if (!StringUtilsXiao.isBlank(createTime)) {
      String[] createTimeList = createTime.split(",");

      if (createTimeList[0].equals(createTimeList[1])) {
        createTimeList[1] = String.valueOf((Long.valueOf(createTimeList[1]) + 24 * 60 * 60 * 1000) - 1);
      }
      paramMap.put("createTimeStart", createTimeList[0]);
      paramMap.put("createTimeEnd", createTimeList[1]);
    }

    paramMap.put("userId", userId);
    paramMap.put("pLiable", pLiable);
    paramMap.put("unitId", unitIds);
    paramMap.put("depositAddress", depositAddress);

    List<Goods> list = baseMapper.loseMoneyGoods(paramMap);

    return list;
  }
}
